import 'package:flutter/material.dart';
import 'package:lib_core/lib_core.dart';
import 'package:lib_uikit/theme/gt_colors.dart';
import 'package:lib_uikit/theme/gt_colors_v3.dart';

class GTThemeProvider extends ChangeNotifier {
  /// 单例
  static final GTThemeProvider _instance = GTThemeProvider._internal();

  static GTThemeProvider get instance => _instance;

  /// 私有的命名构造函数
  GTThemeProvider._internal() {
    // 增加监听 原生侧 Dark 模式更改消息
    GTLibCore.addAllChangeListener((action, initArgs) {
      GTLogger.debug("GTThemeProvider Listener - action: $action ; args: ${initArgs.toJson()}");
      switch (action) {
        case GTGlobalConfigEventAction.darkMode:
          setDark(initArgs.darkMode);
          break;
        case GTGlobalConfigEventAction.language:
          setLanguage(initArgs.language);
          break;
        case GTGlobalConfigEventAction.hzldMode:
          setHzld(initArgs.hzldMode);
          break;
        default:
          break;
      }
    });
  }

  /// 这块需要 统一到 GTGlobalConfig 中
  /// 是否 红涨绿跌
  bool _isHzld = false;

  /// 是否左右互换
  bool _isRTL = false;

  /// 是否深色模式
  bool _isDark = false;

  bool? _isForceDark;

  /// 判断 红涨绿跌
  bool isHzld() => _isHzld;

  /// 设置红涨绿跌
  void setHzld(bool isHzld) {
    _isHzld = isHzld;
    notifyListeners();
  }

  /// 判断 是否左右互换
  bool isRTL() => _isRTL;

  /// 设置左右互换
  void setRTL(bool isRTL, {bool notify = true}) {
    if (isRTL == _isRTL) {
      return;
    }
    _isRTL = isRTL;
    if (notify) {
      notifyListeners();
    }
  }

  /// 判断是否 Dark Mode
  bool isDark() {
    return _isForceDark != null ? _isForceDark! : _isDark;
  }

  /// 设置主题模式
  void setForceDark(bool isForceDark) {
    _isForceDark = isForceDark;

    GTLogger.debug("GTThemeProvider updateForceDarkMode isDark: $_isForceDark");
  }

  bool? isForceDark() {
    return _isForceDark;
  }

  /// 获取主题模式
  ThemeMode getThemeMode() {
    return _isDark ? ThemeMode.dark : ThemeMode.light;
  }

  /// 设置主题
  void setDark(bool isDark) {
    _isDark = isDark;

    GTLogger.debug("GTThemeProvider updateDarkMode isDark: $_isDark");
    notifyListeners();
  }

  /// 获取主题
  ThemeData getTheme({bool isDarkMode = false}) {
    GTLogger.debug("GTThemeProvider getTheme() isDark: $isDarkMode");

    var themeData = ThemeData(
        fontFamily: 'GTFontFamily',
        package: 'lib_uikit',
        brightness: isDarkMode ? Brightness.dark : Brightness.light,
        primaryColor: GTColors.uikitMainHeader.get(isDarkMode),
        // TODO accentColor 已经废弃
        // accentColor: GTColors.uikitGradient7End.get(isDarkMode),
        hintColor: GTColors.uikitGradient7End.get(isDarkMode),
        scaffoldBackgroundColor: GTColors.uikitMainHeader.get(isDarkMode),
        canvasColor: GTColors.uikitMainHeader.get(isDarkMode),
        cardColor: GTColors.uikitCard1.get(isDarkMode),
        dividerColor: GTColors.uikitDivider1.get(isDarkMode),
        tabBarTheme: TabBarTheme(
          dividerColor: Colors.transparent,
          overlayColor: MaterialStateProperty.all<Color>(Colors.transparent),
        ),
        dividerTheme: DividerThemeData(
          color: GTColors.uikitDivider1.get(isDarkMode),
          thickness: 0.5,
          space: 0.0,
          indent: 0.0,
          endIndent: 0.0,
        ),
        textSelectionTheme: TextSelectionThemeData(
          cursorColor: GTColorsV3.text5.color,
          selectionColor: GTColors.uikitPd210p.light,
          selectionHandleColor: GTColors.uikitFunct6.color,
        ),
        indicatorColor: GTColors.textSelection.get(isDarkMode),
        appBarTheme: AppBarTheme(
          titleTextStyle: TextStyle(
            color: GTColors.uikitMainHeaderText.get(isDarkMode),
            fontSize: 18.0,
            fontWeight: FontWeight.bold,
          ),
        ),
        textTheme: TextTheme(
          bodyMedium: TextStyle(
            fontSize: 14,
            color: GTColors.uikitMainHeaderText.get(isDarkMode),
          ),
          bodySmall: TextStyle(
            fontSize: 14,
            color: GTColors.uikitColor333333_E6E6E6.get(isDarkMode),
          ),
        ));

    return themeData;
  }

  // 当前语言环境
  String _language = "en";

  /// 设置主题
  void setLanguage(String language) {
    _language = language;

    GTLogger.debug("GTThemeProvider updateLanguage language: $_language");
    notifyListeners();
  }

  String get language {
    return _language;
  }

  bool get isChina {
    return _language == 'zh-Hans' || _language == 'zh-Hant-TW' || _language == 'cn' || _language == 'tw';
  }

  /// 获取语言
  Locale getLocale() {
    GTLogger.debug("GTThemeProvider getLocale() language: $_language");

    if (_language == 'cn') {
      return const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans');
    } else if (_language == 'tw') {
      return const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant');
    } else if (_language == 'in') {
      return const Locale.fromSubtags(languageCode: 'id');
    } else if (_language == 'br' || _language == 'pt-BR') {
      // return const Locale.fromSubtags(languageCode: 'br');
      return const Locale.fromSubtags(languageCode: 'pt', countryCode: 'BR');
    } else if (_language == 'pt-PT') {
      return const Locale.fromSubtags(languageCode: 'pt', countryCode: 'PT');
    } else {
      if (_language == 'ar') {
        setRTL(true, notify: false);
      }
      return Locale.fromSubtags(languageCode: _language);
    }
  }
}
